﻿using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.StorageClient;

using Bowlus.Common;

namespace Bowlus.AppHost
{
    public class StorageAppRepository
    {
        private CloudBlobContainer container;

        public StorageAppRepository()
        {
            container = BowlusSettings.StorageAccount.CreateCloudBlobClient().GetContainerReference(
                BowlusSettings.AppContainer);

            //Initialize 7Zip Library Wrapper
            SevenZip.SevenZipBase.SetLibraryPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "7zip\\7z.dll"));
        }

        public void WriteAppTo(string app, string destination)
        {
            var blob = container.GetBlobReference(app);

            using (var stream = new MemoryStream())
            {
                blob.DownloadToStream(stream);

                var extractor = new SevenZip.SevenZipExtractor(stream);
                extractor.ExtractArchive(destination);
            }
        }
    }

}
